Proper way to handle pthread communication / signals in this instance?

I'm writing a small client / server demo that shares files between peers. One a peer gets a list of ip addresses from the main server, the main thread creates a thread for each respective file. The process looks like this:

  • Main thread gets list of files from server
  • Thread created for each file (detached)
  • In each created thread, connect to the peers specified / associated with a file
  • Thread downloads the file in chunks
  • Thread announces the file was complete

My problem comes into play when trying to "query" a thread. In each thread, I keep track of the progress of a transfer. In my main thread, I would like the user to be able to see the progress of all of the transfers taking place. What would be the best way to do so? I was thinking about sending a signal using pthread_kill to each thread respectively, although it seems like there should be a better way. If anyone has an idea, I'd love to hear it.

Answers


When you create your thread, you include a void * to point to anything you wish. In your example, you could declare an array of progress values and pass the address of one of them to each thread you create, let the thread perform a simple update when it needs to, and your main thread can periodically check the values.

If you're already using that parameter for something, you will need to create a structure comprising this new value and whatever you're already using, and pass the address of it so the thread gets everything it needs.


Need Your Help

UISearchBar Cancel Button Not Working

ios uisearchbar

I have a UISearchBar on my MKMapView that I'm going to use to search annotations. I'm having trouble getting the cancel button to work. I create the search bar in my viewDidLoad method like this:

SQL Joins : in, out, shake it all about

sql-server join inner-join outer-join

I am performing the follwing sql to return a data where there is a match of both dob and address in tables1 & 2.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.