Search Results

Search found 6376 results on 256 pages for 'launch condition'.

Page 51/256 | < Previous Page | 47 48 49 50 51 52 53 54 55 56 57 58  | Next Page >

  • Nested if statements or not

    - by Zaps
    Hi, I hope this hasn't been asked before. I have a nullable boolean called boolIsAllowed and a if condition like so: if(boolIsAllowed.HasValue && boolIsAllowed.Value) { //do something } My question is this good code or would I be better separating it into a nested if statement? Will the second condition get checked if boolIsAllowed.HasValue is equal to false and then throw an exception? I hope this question is too stupid. Thanks in advance.

    Read the article

  • Microbenchmark showing process-switching faster than thread-switching; what's wrong?

    - by Yang
    I have two simple microbenchmarks trying to measure thread- and process-switching overheads, but the process-switching overhead. The code is living here, and r1667 is pasted below: https://assorted.svn.sourceforge.net/svnroot/assorted/sandbox/trunk/src/c/process_switch_bench.c // on zs, ~2.1-2.4us/switch #include <stdlib.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <semaphore.h> #include <unistd.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/time.h> #include <pthread.h> uint32_t COUNTER; pthread_mutex_t LOCK; pthread_mutex_t START; sem_t *s0, *s1, *s2; void * threads ( void * unused ) { // Wait till we may fire away sem_wait(s2); for (;;) { pthread_mutex_lock(&LOCK); pthread_mutex_unlock(&LOCK); COUNTER++; sem_post(s0); sem_wait(s1); } return 0; } int64_t timeInMS () { struct timeval t; gettimeofday(&t, NULL); return ( (int64_t)t.tv_sec * 1000 + (int64_t)t.tv_usec / 1000 ); } int main ( int argc, char ** argv ) { int64_t start; pthread_t t1; pthread_mutex_init(&LOCK, NULL); COUNTER = 0; s0 = sem_open("/s0", O_CREAT, 0022, 0); if (s0 == 0) { perror("sem_open"); exit(1); } s1 = sem_open("/s1", O_CREAT, 0022, 0); if (s1 == 0) { perror("sem_open"); exit(1); } s2 = sem_open("/s2", O_CREAT, 0022, 0); if (s2 == 0) { perror("sem_open"); exit(1); } int x, y, z; sem_getvalue(s0, &x); sem_getvalue(s1, &y); sem_getvalue(s2, &z); printf("%d %d %d\n", x, y, z); pid_t pid = fork(); if (pid) { pthread_create(&t1, NULL, threads, NULL); pthread_detach(t1); // Get start time and fire away start = timeInMS(); sem_post(s2); sem_post(s2); // Wait for about a second sleep(1); // Stop thread pthread_mutex_lock(&LOCK); // Find out how much time has really passed. sleep won't guarantee me that // I sleep exactly one second, I might sleep longer since even after being // woken up, it can take some time before I gain back CPU time. Further // some more time might have passed before I obtained the lock! int64_t time = timeInMS() - start; // Correct the number of thread switches accordingly COUNTER = (uint32_t)(((uint64_t)COUNTER * 2 * 1000) / time); printf("Number of process switches in about one second was %u\n", COUNTER); printf("roughly %f microseconds per switch\n", 1000000.0 / COUNTER); // clean up kill(pid, 9); wait(0); sem_close(s0); sem_close(s1); sem_unlink("/s0"); sem_unlink("/s1"); sem_unlink("/s2"); } else { if (1) { sem_t *t = s0; s0 = s1; s1 = t; } threads(0); // never return } return 0; } https://assorted.svn.sourceforge.net/svnroot/assorted/sandbox/trunk/src/c/thread_switch_bench.c // From <http://stackoverflow.com/questions/304752/how-to-estimate-the-thread-context-switching-overhead> // on zs, ~4-5us/switch; tried making COUNTER updated only by one thread, but no difference #include <stdlib.h> #include <stdint.h> #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <sys/time.h> uint32_t COUNTER; pthread_mutex_t LOCK; pthread_mutex_t START; pthread_cond_t CONDITION; void * threads ( void * unused ) { // Wait till we may fire away pthread_mutex_lock(&START); pthread_mutex_unlock(&START); int first=1; pthread_mutex_lock(&LOCK); // If I'm not the first thread, the other thread is already waiting on // the condition, thus Ihave to wake it up first, otherwise we'll deadlock if (COUNTER > 0) { pthread_cond_signal(&CONDITION); first=0; } for (;;) { if (first) COUNTER++; pthread_cond_wait(&CONDITION, &LOCK); // Always wake up the other thread before processing. The other // thread will not be able to do anything as long as I don't go // back to sleep first. pthread_cond_signal(&CONDITION); } pthread_mutex_unlock(&LOCK); return 0; } int64_t timeInMS () { struct timeval t; gettimeofday(&t, NULL); return ( (int64_t)t.tv_sec * 1000 + (int64_t)t.tv_usec / 1000 ); } int main ( int argc, char ** argv ) { int64_t start; pthread_t t1; pthread_t t2; pthread_mutex_init(&LOCK, NULL); pthread_mutex_init(&START, NULL); pthread_cond_init(&CONDITION, NULL); pthread_mutex_lock(&START); COUNTER = 0; pthread_create(&t1, NULL, threads, NULL); pthread_create(&t2, NULL, threads, NULL); pthread_detach(t1); pthread_detach(t2); // Get start time and fire away start = timeInMS(); pthread_mutex_unlock(&START); // Wait for about a second sleep(1); // Stop both threads pthread_mutex_lock(&LOCK); // Find out how much time has really passed. sleep won't guarantee me that // I sleep exactly one second, I might sleep longer since even after being // woken up, it can take some time before I gain back CPU time. Further // some more time might have passed before I obtained the lock! int64_t time = timeInMS() - start; // Correct the number of thread switches accordingly COUNTER = (uint32_t)(((uint64_t)COUNTER * 2 * 1000) / time); printf("Number of thread switches in about one second was %u\n", COUNTER); printf("roughly %f microseconds per switch\n", 1000000.0 / COUNTER); return 0; }

    Read the article

  • Format the output of a string

    - by prince23
    hi here string strScore="2"; or string strScore="2.45656" now here i am checking the condition if it is double value = double.Parse(strScore); strScore = value.ToString("##.##"); 2.45656 like this then i am showing the output as 2.45 if the input is string strScore="2"; then the ouput is shown as "2" but now i need to show the output has 2.00 how can i format the code like this based on the condition

    Read the article

  • DRY up Ruby ternary

    - by Reed G. Law
    I often have a situation where I want to do some conditional logic and then return a part of the condition. How can I do this without repeating the part of the condition in the true or false expression? For example: ClassName.method.blank? ? false : ClassName.method Is there any way to avoid repeating ClassName.method? Here is a real-world example: PROFESSIONAL_ROLES.key(self.professional_role).nil? ? 948460516 : PROFESSIONAL_ROLES.key(self.professional_role)

    Read the article

  • How to pick random (small) data samples using Map/Reduce?

    - by Andrei Savu
    I want to write a map/reduce job to select a number of random samples from a large dataset based on a row level condition. I want to minimize the number of intermediate keys. Pseudocode: for each row if row matches condition put the row.id in the bucket if the bucket is not already large enough Have you done something like this? Is there any well known algorithm? A sample containing sequential rows is also good enough. Thanks.

    Read the article

  • Redundant code constructs

    - by Diomidis Spinellis
    The most egregiously redundant code construct I often see involves using the code sequence if (condition) return true; else return false; instead of simply writing return (condition); I've seen this beginner error in all sorts of languages: from Pascal and C to PHP and Java. What other such constructs would you flag in a code review?

    Read the article

  • How to dynamically import javascript and css files

    - by marcos
    i Want to import a given css or javascript file depending os some conditions, in my Servlet i have: protected void doPost(...) { if(condition) { //import some javascript or css file here } } I need this behavior since i have too many files to import and the files name may vary according to the condition Is it possible?

    Read the article

  • Dynamically startup form

    - by comii
    Hi! I have application in VB.net that have two different form (Form1 and Form2). Now I need to examine some condition and if condition is true then i set Form1 for startup for and if it not then i set Form2 for startup form. So is there anyway to dynamically call startup form? Thanks!

    Read the article

  • Netbeans: How do I break on a NullPointerException?

    - by darren
    Hi When debugging a java program in netbeans, I want the debugger to stop on the line that causes a NullPointerException so I can examine the variables there. I believe I have to use a condition on the breakpoint set at that line, but what is the syntax of the condition? thanks for the help

    Read the article

  • What's the best way to handle modules that use each other?

    - by Axeman
    What's the best way to handle modules that use each other? Let's say I have a module which has functions for hashes: # Really::Useful::Functions::On::Hash.pm use base qw<Exporter>; use strict; use warnings; use Really::Useful::Functions::On::List qw<transform_list>; our @EXPORT_OK = qw<transform_hash transform_hash_as_list ...>; #... sub transform_hash { ... } #... sub transform_hash_as_list { return transform_list( %{ shift() } ); } #... 1 And another module has been segmented out for lists: # Really::Useful::Functions::On::List.pm use base qw<Exporter>; use strict; use warnings; use Really::Useful::Functions::On::Hash qw<transform_hash>; our @EXPORT_OK = qw<transform_list some_func ...>; #... sub transform_list { ... } #... sub some_func { my %params = transform_hash @_; #... } #... 1 Suppose that enough of these utility functions are handy enough that I'll want to use them in BEGIN statements and import functions to process parameter lists or configuration data. I have been putting sub definitions into BEGIN blocks to make sure they are ready to use whenever somebody includes the module. But I have gotten into hairy race conditions where a definition is not completed in a BEGIN block. I put evolving code idioms into modules so that I can reuse any idiom I find myself coding over and over again. For instance: sub list_if { my $condition = shift; return unless $condition; my $more_args = scalar @_; my $arg_list = @_ > 1 ? \@_ : @_ ? shift : $condition; if (( reftype( $arg_list ) || '' ) eq 'ARRAY' ) { return wantarray ? @$arg_list : $arg_list; } elsif ( $more_args ) { return $arg_list; } return; } captures two idioms that I'm kind of tired of typing: @{ func_I_hope_returns_a_listref() || [] } and ( $condition ? LIST : ()) The more I define functions in BEGIN blocks, the more likely I'll use these idiom bricks to express the logic the more likely that bricks are needed in BEGIN blocks. Do people have standard ways of dealing with this sort of language-idiom-brick model? I've been doing mostly Pure-Perl; will XS alleviate some of this?

    Read the article

  • How to create a matrix format in perl

    - by shaq
    I have an array of array in which each array is like: clusterA gene1 1 clusterA gene2 0 clusterB gene1 1 clusterB gene2 0 I want to produce a file like: name gene1 gene2 clusterA 1 0 clusterB 1 0 Current attempt: if (condition) { @array = ($cluster, $genes, "1"); } elsif (not condition) { @array = ($cluster, $genes, "0"); } push @AoA, [ @array ]; @A0A is my array of array.

    Read the article

  • Get Row of DataGrid in User Control Page

    - by Romil
    My Question is related to access the rows in one page and putting conditions in another page. I need to check whether a datagrid has row in it or not. DataGrid is in .aspx page. Based on this checking i need to write a condition in .ascx page. the .ascx on which condition is checked is linked to .aspx page. Meaning that UserControl1.ascx is Register with Default.aspx page I am using VS 2003 Please advice Thanks

    Read the article

  • on and off the user defined notification in status bar

    - by sairam333
    Hi i am displaying some alert message in the status bar depending up on my application condition.up to this the alert is displayed properly.But now i want if notification icon is appeared in the status bar when i saw once it(icon) will be removed from the status bar .and again it will be appeared depending up on condition .For this what can i do Give me some suggestions.Thanks in advance

    Read the article

  • Streaming HTTP response, flushing to the browser

    - by synic
    I've got a view like the following: from django.views.decorators.http import condition def stream(): for i in range(0, 40): yield " " * 1024 yield "%d" % i time.sleep(1) @condition(etag_func=None): def view(request): return HttpResponse(stream(), mimetype='text/html') However, it definitely doesn't seem to be streaming at all. All the data is dumped at once, at the end, after about 40 seconds. How can I get it to flush correctly?

    Read the article

  • Running C++ functions simultaneously

    - by user2974881
    My code is similar to the following: int main() { values(); } int values() { if (condition) { 'code' } else if (condition) { 'code' } else { 'code' } motors(); } int motors() { 'code' } motors() needs values from values() to run. What could I do so that values() and motors() run simultaneously, side by side, and keep running until the user exits out of the program?

    Read the article

  • How can I program the function keys on my MacBook?

    - by mrdatabase
    I'd like to assign basic tasks to the function keys (or other keys if it's easier) on my MacBook. For example I'd like the following to happen when F1 (or whatever key) is pressed: Launch Safari Open a new window Navigate to some specified site Another example could be: Launch Xcode Open some specific project I've experimented with the Automator for things like re-naming dozens of files in a particular directory but I don't know where to start for the above examples.

    Read the article

  • How do I prevent infinite recursion in X11 start-up process?

    - by chrisaycock
    I wasn't able to run X11 or Terminal after rebooting my Mac. After digging around, I got them to work when I commented-out this line in my .cshrc: xset b off It appears that xset will attempt to launch X11 if it isn't running already, and since X11 will launch the default shell through xterm and thus encounter the xset line above, we will have an infinite loop. I would like to keep the above line in my .cshrc. Is there a way to prevent X11 from launching itself?

    Read the article

< Previous Page | 47 48 49 50 51 52 53 54 55 56 57 58  | Next Page >