473,323 Members | 1,589 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,323 software developers and data experts.

multithreading programing in c

here i have created a processFunction which will create another thread when receiverfunction will send signal pthread_cond_signal(&recvMutex) and there is a handlerprocess which is created by processfunction will will send the signal after finishing its job to senderFunction. Finally the senderFunction will print the data to the console.

but here i have some problem. weather it ok to create the thread inside this function. please some body help me


Expand|Select|Wrap|Line Numbers
  1.  
  2. void *ProcessFunction(void *data)
  3. {
  4.  
  5.     int retVal;
  6.     pthread_t handlerThread[MAX_THREAD];
  7.     retVal = pthread_cond_init(&contCond, NULL);
  8.     checkerror(retVal);
  9.  
  10.     /* initalizeing thread attribute*/
  11.     retVal = pthread_attr_init(&sendAttr);
  12.     checkerror(retVal);
  13.  
  14.     retVal = pthread_attr_setdetachstate(&sendAttr, PTHREAD_CREATE_JOINABLE);
  15.     checkerror(retVal);
  16.  
  17.     while(1)
  18.     {
  19.  
  20.         pthread_mutex_lock(&recvMutex);
  21.         printf("process is Waiting for mutex\n");
  22.  
  23.         /* waiting for receive */
  24.         pthread_cond_wait(&recvCond, &recvMutex);
  25.         printf("The word is %s\n",information);
  26.  
  27.         pthread_mutex_lock(&contCond);
  28.         static int cont = 0;
  29.         cont++;
  30.         pthread_mutex_unlock(&contCond);
  31.  
  32.         retVal = pthread_create(&handlerThread[cont],&sendAttr, handlerProcess, (void *)cont);
  33.         checkerror(retVal);
  34.         pthread_join(handlerThread[cont], NULL);
  35.  
  36.         pthread_mutex_unlock(&recvMutex);
  37.     }
  38.     sleep(1);
  39.     return 0;
  40. }
  41.  


<br/>

Expand|Select|Wrap|Line Numbers
  1. void *handlerProcess(void *data)
  2. {
  3.  
  4.     pthread_mutex_lock(&sendMutex);
  5.     printf("This thread_id is going to exit %d\n",(int data);
  6.     strcat(information,"added rabi\n");
  7.     //sleep(1);                /* checking for processFunction is waiting for this handlerProcess */
  8.     data--;
  9.     pthread_cond_signal(&sendCond);
  10.     pthread_mutex_unlock(&sendMutex);
  11.     pthread_exit(NULL);
  12.     return 0;
  13. }
  14.  
Apr 23 '12 #1
1 1580
weaknessforcats
9,208 Expert Mod 8TB
It doesn't matter where you create your thread. The thread create will return so your creatng function can return.

Meanwhile the thread is a function that is running out there independently of the main thread of your program. The thread will continue to run until the thread function completes.

Probably you need to set up an event handler in the thread so you can launch events from the main thread to tell your worker thread what to do.
Apr 23 '12 #2

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

Similar topics

2
by: Brzezi | last post by:
Hi. I`m mostly java programmer, but I know a C++ too. Now I have to write multithread application in C++. Which library for multithread programing would you recomend for me? if you know that I...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
by: Shllpp 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.