473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to use this for killing array of threads?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<signal.h>
  3. #include<pthread.h>
  4.  
  5. void *print1(void *tid) 
  6. {
  7.     pthread_t *td= tid;
  8.     pthread_mutex_t lock1=PTHREAD_MUTEX_INITIALIZER;
  9.     pthread_mutex_lock(&lock1);
  10.     printf("1");
  11.     printf("2");
  12.     printf("3");
  13.     printf("4\n");
  14.     printf("Coming out of thread1 \n");
  15.     sleep(2);
  16.     pthread_mutex_unlock(&lock1);
  17.     pthread_kill(*td,SIGKILL);//killing remaining all threads 
  18.     return NULL;
  19. }
  20. void *print2(void *arg) 
  21. {
  22.     pthread_mutex_t *lock = arg;
  23.     pthread_mutex_lock(lock);
  24.     sleep(5);        
  25.     printf("5");
  26.     sleep(5);
  27.     printf("6");
  28.     sleep(5);
  29.     printf("7");
  30.     sleep(5);
  31.     printf("8\n");
  32.     fflush(stdout);
  33.     pthread_mutex_unlock(lock);
  34.     return NULL;
  35. }
  36. int main() 
  37. {
  38.     int s;
  39.     pthread_t tid1, tid2;
  40.     pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  41.     printf("creating Thread 1and 2 \n");
  42.     sleep(2);
  43.     pthread_create(&tid1, NULL, print1,&tid2);
  44.     pthread_create(&tid2, NULL, print2,&lock);
  45.     printf("Running Thread 1\n");
  46.     sleep(2);
  47.     pthread_join(tid1, NULL);
  48.     pthread_join(tid2, NULL);
  49.     return 0;
  50. }
Nov 29 '14 #1
0 1147

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Jacek Pop³awski | last post by:
Hello. I am going to write python script which will read python command from socket, run it and return some values back to socket. My problem is, that I need some timeout. I need to say for...
4
by: muscha | last post by:
Hi, I spawns a number of background threads while creating a windows forms application. I noticed that when I quit the application or calling Application.Exit, it hasn't really stop. Is there a...
2
by: Tom | last post by:
hi friends is there a way to terminate all threads in one go ? Thanks Tom
5
by: | last post by:
Hi I have thread that recives event notifications from a server. the thread is created as a managed thread. But when using Thread Abort the thread doesn't terminate. the thread is in an...
6
by: Bob Day | last post by:
VS 2003, vb.net ... Is there an easy way to kill from the main form all threads running ? Is there a thread collection to iterate through to kill each one? I know each thread's name, so could...
2
by: mmitchell | last post by:
I have a service that executes a method in a dll. This method spawns several threads. When I stop the service I notice the process doesn't stop right away. I assume the threads that some time to...
8
by: Rob | last post by:
Hello, I've got an issue where a process in a third party application has a dll which while exiting sort of freezes and runs away with processor cycles. i've written a block of code so that I...
3
by: michdoh | last post by:
Hi All I'm looking for some help on creating a basic multi threaded application. i'n new to threads in this environment so for test purposes I've produced a very basic program. (which doesn't...
41
by: Carl J. Van Arsdall | last post by:
Hey everyone, I have a question about python threads. Before anyone goes further, this is not a debate about threads vs. processes, just a question. With that, are python threads reliable? Or...
6
by: Roger Heathcote | last post by:
sjdevnull@yahoo.com wrote: <snip> Fair point, but for sub processes that need to be in close contact with the original app, or very small functions that you'd like 100s or 1000s of it seems...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.