Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pthreads problem

Posted on 1999-10-07
5
Medium Priority
?
323 Views
Last Modified: 2013-12-27
Hello,

I am using pthreads library under Sun OS.
This is my code.

#include <stdio.h>
#include <pthread.h>

pthread_t ThreadId;
FILE *out;
int v=5;
void *loop(void *param)
{
      
      int *value=(int *) param;
      
      fprintf(out,"value = %d",*value);


}



void main(void)
{
      int i;
            
      out = fopen("out.txt","w");
      pthread_create(&ThreadId,NULL, loop, (void *) &v);
      for(i=0;i<3000000;i++);
      
      fclose(out);
}


Why is not the body of 'loop' function executed?

Thanks.
0
Comment
Question by:jjescri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
gurpreets earned 800 total points
ID: 2108909
You should return void pointer in the function loop.
Just include the following line -

return NULL;

In the main routine, before fclose include the following stmt -

pthread_join(ThreadId, NULL);

Now compile the program in the following fashion -

cc <filename> -D_REENTRANT -lpthread -mt

This would produce an execurable which will do what u want. For your reference I am including the whole modified code -

#include <stdio.h>
#include <pthread.h>

pthread_t ThreadId;
FILE *out;
int v=5;

void *loop(void *param)
{

    int *value=(int *) param;

    fprintf(out,"value = %d",*value);

    return param;
}

void main(void)
{

    int i;

    out = fopen("out.txt","w");

    pthread_create(&ThreadId,NULL, loop, (void *) &v);
    for(i=0;i<3000000;i++);
    pthread_join(ThreadId, NULL);

    fclose(out);
}
0
 

Author Comment

by:jjescri
ID: 2109120
Gurpreets,

Your code does not work.
The file out.txt is void after the execution of the program.

0
 
LVL 2

Expert Comment

by:gurpreets
ID: 2109166
jjescri,

The code is working properly in  my system. Follow the instructions properly. The only significant change which I have done to your code, is the inclusion of pthread_join.

0
 

Author Comment

by:jjescri
ID: 2109197
Gurpreets,

I am using g++ as compiler.
The program finishes never.

Thanks
0
 
LVL 2

Expert Comment

by:gurpreets
ID: 2109227
I have compiled using the SUN C++ compiler version 4.2. Everything is working fine.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question