LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2005, 08:58 AM   #1
maritos
LQ Newbie
 
Registered: Oct 2005
Posts: 5

Rep: Reputation: 0
Threads Problem


Hi, i have a little problem in my code .

Quote:

void thread_exit() {
thread_t running_thread = list->element;
thread_t next_thread = list->next->element;
remove_list(&list, running_thread);
thread_switch(running_thread->context, next_thread->context);
}
This function terminate a thread that is running, removing her from the list, and
after removing her, switch the execution context of the thread with the next thread on the list. The problem is that i have to free the structure(running_thread) and her context, but i cant do it before the thread_switch(), neither immediately after, so how can i resolve this problem.

Last edited by maritos; 11-27-2005 at 08:59 AM.
 
Old 11-28-2005, 11:34 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
I use a list implementation that I once found in a book.
The node is defined as:
Code:
struct llnode
{
  struct llnode *prev;  // previous element
  struct llnode *next;  // next element
  void *info;           // pointer to the struct that keeps the actual info (let's say struct xx)
  int len;              // size of struct xx
};
The function remove_list (in my case ll_remove) will remove the node from the list but not free the info pointer (I guess that my info is your context).
The function ll_remove will return the info pointer. You can use that pointer for the thread_switch and free it afterwards (either after the call to thread_switch or maybe in thread_switch).
Code:
void * ll_remove(struct llhead *head, struct llnode *node)
{
void *info;

  if(node==NULL)
    return NULL;

  info=node->info;

  // move some pointers around
  // so the node is removed
  .....
  // free the memory occupied by the node
  free(node);

  return info;
}
Your modified code will look like:
Code:
void thread_exit() {
  void *context;
  thread_t running_thread = list->element;
  thread_t next_thread = list->next->element;
  context=(void*)remove_list(&list, running_thread);
  thread_switch(context, next_thread->context);
  free(context);
}

Last edited by Wim Sturkenboom; 11-29-2005 at 12:02 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Problem with threads in linux branens Programming 1 02-13-2005 07:45 PM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
I have a problem with POSIX Threads. AndreasA Programming 2 08-11-2003 02:48 PM
A problem on subscription to threads koyi LQ Suggestions & Feedback 2 07-25-2003 12:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration