Say I create a pthread as pthread_t lift_3; and pthread_create(&lift_1, NULL, lift, share);. When it goes into lift(), how can I get it the function to print the actual name of the thread? Or set a name for the thread?
I have tried using pthread_self() to acquire the id, but it instead gives random numbers
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* lift(void* ptr)
{
printf("thread name = %c\n", pthread_self());
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t lift_1; // declare thread
pthread_create(&lift_1, NULL, lift, NULL);
pthread_join(lift_1, NULL);
return 0;
}
The expected outcome should be thread name = lift_1
You're looking for the "name of the function that the thread started in".
There is no such thing as "thread name".
When calling pthread_self, you get the "id" of the thread, which something like a randomly-generated name.
To simulate the desired behavior in the past, I wrote the following code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// This lines means a variable that is created per-thread
__thread const char* thread_name;
void* lift(void* ptr)
{
// Paste this line in the beginning of every thread routine.
thread_name = __FUNCTION__;
// Note two changes in this line
printf("thread name = %s\n", thread_name);
pthread_exit(NULL);
return NULL;
}
int main()
{
// Added line
thread_name = __FUNCTION__;
pthread_t lift_1; // declare thread
pthread_create(&lift_1, NULL, lift, NULL);
pthread_join(lift_1, NULL);
//Added line
printf("Original thread name: %s\n", thread_name);
return 0;
}
Related
I've been trying to create a chat program in C with threads but it hasn't been working, so I decided to play with threads for a bit first. I'm trying to run a thread that prints "hello world", but it gives me a segmentation fault. I haven't been able to find the root of the problem so I came here. heres the code:
#include <stdio.h>
#include <pthread.h>
void* test(void * arg) {
printf("hello world\n");
return NULL;
}
int main() {
pthread_t test;
pthread_create(&test, NULL, (void *) test, NULL);
pthread_exit(NULL);
return 0;
}
There's probably a stupid reason why it's not working so I hope you guys wont have too much trouble finding it!
Your start routine name and thread id name are the same so I think compiler gets confused when you pass &test. Your code works by changing the thread id name.
#include <stdio.h>
#include <pthread.h>
void* test(void * arg) {
printf("hello world\n");
return NULL;
}
int main() {
pthread_t t;
pthread_create(&t, NULL, (void *) test, NULL);
pthread_exit(NULL);
return 0;
}
void* test(void * arg) { ... }
// vvvv
pthread_t test;
pthread_create(&test, NULL, (void *) test, NULL);
// ^^^^
That pthread_t variable is "hiding" the function name. In other words, you're calling some arbitrary uninitialised value as your function. That's unlikely to end well :-)
What you're doing is really no different to expecting the following program to output 7 (it won't):
#include <stdio.h>
int i = 7;
int main(void) {
int i = 42;
printf("%d\n", i);
return 0;
}
You could solve it simply by renaming your function to testFn, for example.
In the sample below, I tried to modify a char pointer in the main process using pthread. However, I dont see the pointer value changed. What is the reason and how can I achieve my goal?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
#include <errno.h>
#include <string.h>
char *target = "thread";
void *modify(void *vargp) {
vargp = target;
printf("Thread vargp = %s\n", (char*)vargp);
return NULL;
}
int main() {
char *pt = "main";
pthread_t thread_id;
printf("Before Thread, pt = %s\n", pt);
pthread_create(&thread_id, NULL, modify, pt);
pthread_join(thread_id, NULL);
printf("After Thread, pt = %s\n", pt);
exit(0);
}
$ ./pthread_simple
Before Thread, pt = main
Thread vargp = thread
After Thread, pt = main
What I want to achieve is:
$ ./pthread_simple
Before Thread, pt = main
Thread vargp = thread
After Thread, pt = thread
Function arguments are copies of what are passed and change to them is local to the functions. To have functions modify things, you have to pass pointers to what should be modified.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
#include <errno.h>
#include <string.h>
char *target = "thread";
void *modify(void *vargp) {
*(char**)vargp = target; /* add cast and dereference to modify what is pointed */
printf("Thread vargp = %s\n", *(char**)vargp); /* add cast and dereference */
return NULL;
}
int main() {
char *pt = "main";
pthread_t thread_id;
printf("Before Thread, pt = %s\n", pt);
pthread_create(&thread_id, NULL, modify, &pt); /* pass pointer to pt */
pthread_join(thread_id, NULL);
printf("After Thread, pt = %s\n", pt);
exit(0);
}
I wrote this program to practice pthread system calls so I used some printing lines to check the results but they are escaped the output is:
Thread 1 created
Thread 2 created
test3
while I think it should be
thread 1 created
test2
thread 2 created
test3
test1
The order may change but I should have this lines so why it escape this print statements?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *function();
void *function2();
int main(int argc, char *argv[])
{
pthread_t tid;
int rc;
rc = pthread_create(&tid, NULL, function(), NULL);
if(rc > 0){
fprintf(stderr, "Error\n");
exit(1);
}
pthread_join(tid, NULL);
sleep(1);
printf("test1\n");
pthread_exit(NULL);
}
void *function(){
int rc;
pthread_t tid;
printf("Thread 1 created\n");
rc = pthread_create(&tid, NULL, function2(), NULL);
if(rc > 0){
fprintf(stderr, "Error\n");
exit(1);
}
printf("test2\n");
pthread_exit(NULL);
}
void *function2(){
pthread_detach(pthread_self());
printf("Thread 2 created\n");
printf("test3\n");
pthread_exit(NULL);
}
rc = pthread_create(&tid, NULL, function(), NULL);
You're trying to call pthread_create() with the pointer returned by calling function() as the function to run in the new thread (Remember, function arguments get evaluated before the function itself is called). Now, function() doesn't actually return any value, but it calls function2() in its body (while evaluating the arguments for another call to pthread_create()), which also doesn't return any value, but does call pthread_exit() instead. Since there's only one thread at that point because only the main process thread is running (pthread_create() hasn't actually been called yet; the call stack looks like main() -> function() -> function2()), the whole program then exits.
You need to call pthread_create() with pointers to function and function2, not the results of calling them:
rc = pthread_create(&tid, NULL, function, NULL);
etc.
The code is sample, what I need to do is to modify.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *my_function (void*); // Function for the thread
int main ()
{
pthread_t my_thread ; // Declare a thread identifier
int rc1, x = 7;
// Create my_thread
if ( (rc1 = pthread_create (& my_thread, NULL, & my_function, (void*) &x)))
{
printf ("Error in creating thread %d\n", rc1);
}
pthread_join ( my_thread, NULL); // wait for thread to exit
return (0); // exit the main function
}
// The my_thread is created with my_function() which accepts an argument
void *my_function(void* arg)
{
int i = *(int*)arg;
printf ("The argument which this thread received is %d \n", i ) ;
pthread_exit (NULL) ; // thread exits
}
The question is: Pass a simple integer to a thread’s start function at thread creation time. But I don't know how to pass a simple integer to a thread's start function, and I also don't know what is thread creation time.
If your imlementation has intptr_t (most of them do), you can modify the code as following:
int rc1;
intptr_t x = 7
...
pthread_create (&my_thread, NULL, &my_function, (void*)x));
...
void* my_function(void* arg)
{
intptr_t i = (intptr_t)arg;
...
I'm trying to create a thread, and I cannot figure out what I am doing wrong here. It's very basic, I just want to make sure I can get the thread created before I delve into what I'll be doing in the thread. Here's my code.
//prog.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>
int threadCount =0; //Global variable to hold our thread counter
//this is the function that gets called when a thread is created
void *threadCreate(void* arg){
printf("Thread #%d has been created\n", threadCount);
threadCount++;
int param = (int)arg;
printf("We were sent: %d\n", param);
printf("Now the thread will die\n");
threadCount--;
pthread_exit(NULL);
}
//main
int main(int argc, char *argv[]){
pthread_t tid;
int numski = 50;
int res;
res = pthread_create(&tid, NULL, threadCreate, (void*)numski);
if (res){
printf("Error: pthread_create returned %d\n", res);
exit(EXIT_FAILURE);
}
return 0;
}
I am compiling using the following command:
gcc -Wall -pthread -std=c99 prog.c -o Prog
And when I try to run it, I get no output at all.
Main is exiting right away, and therefore your process is dying right away. End it with pthread_join to wait for them. Here is one example I googled, which contains the following example code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}